package top.zhongyy.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import top.zhongyy.pojo.Type;
import top.zhongyy.service.TypeService;

import java.util.List;

/**
 * @Created by IntelliJ IDEA.
 * @DESC:博客类型
 * @Author: Andy
 * @Date: 2021/3/25
 */

@Controller
@RequestMapping("/admin")
@ApiModel(value = "博客分类的后台控制器")
@SuppressWarnings(value = "all")
public class TypeController {
    
    @Autowired
    private TypeService typeService;
    
    /**
     * @param model
     * @param pageNum
     * @author Andy
     * @date: 2021/3/25 20:03
     */
    @ApiOperation(value = "分页显示分类列表")
    @GetMapping("/types")
    public String list(Model model,
                       @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        //1、开启分页
        PageHelper.startPage(pageNum, 5, "id desc");
        List<Type> list = typeService.list();
        //2、封装list到 PageInfo对象中自动分页
        PageInfo<Type> pageInfo = new PageInfo<>(list);
        model.addAttribute("page", pageInfo);
        return "admin/types";
    }
    
    
    /**
     * @param model
     * @author Andy
     * @date: 2021/3/25 20:04
     */
    @ApiOperation(value = "转到新增界面")
    @GetMapping("/types/input")
    public String toAdd(Model model) {
        // 不加会报空指针错误
        model.addAttribute("type", new Type());
        return "admin/types_release";
    }
    
    /**
     * @param type
     * @param attributes
     * @author Andy
     * @date: 2021/3/25 20:59
     */
    @ApiOperation(value = "新增分类")
    @PostMapping("/types")
    public String Add(@Validated Type type, RedirectAttributes attributes) {
        Type type1 = typeService.getTypeBtName(type.getName());
        if (type1 != null) {
            attributes.addFlashAttribute("message", "不能添加重复的分类");
            return "redirect:/admin/types/input";
        }
        boolean save = typeService.save(type);
        if (save) {
            attributes.addFlashAttribute("message", "新增成功");
        } else {
            attributes.addFlashAttribute("message", "新增失败");
        }
        return "redirect:/admin/types";
    }
    
    /**
     * @param id
     * @param attributes 重定向 addFlashAttribute 方法可以携带此参数
     * @author Andy
     * @date: 2021/3/25 20:58
     */
    @ApiOperation(value = "根据 id 删除分类")
    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable("id") Long id, RedirectAttributes attributes) {
        boolean flag = typeService.removeById(id);
        if (flag) {
            attributes.addFlashAttribute("message", "删除成功");
        } else {
            attributes.addFlashAttribute("message", "删除失败");
        }
        return "redirect:/admin/types";
    }
    
    
    /**
     * @param id
     * @param model
     * @author Andy
     * @date: 2021/3/25 21:06
     */
    @ApiOperation(value = "点击编辑按钮到编辑界面")
    @PostMapping("/admin/types/{id}/input")
    public String toEdit(@PathVariable("id") Long id, Model model) {
        model.addAttribute("type", typeService.getById(id));
        return "admin/types_release";
    }
    
    
    /**
     * @param type
     * @param result     BindingResult 搭配Validated 一起使用 详情百度
     * @param id
     * @param attributes 重定向参数传递
     * @author Andy
     * @date: 2021/3/25 22:38
     */
    @ApiOperation(value = "修改分类提交")
    @PostMapping("/types/{id}")
    public String editPost(@Validated Type type, BindingResult result, @PathVariable("id") Long id, RedirectAttributes attributes) {
        Type type1 = typeService.getTypeBtName(type.getName());
        if (type1 != null) {
            result.rejectValue("name", "nameError", "分类已存在");
        }
        if (result.hasErrors()) {
            return "admin/types_release";
        }
        boolean flag = typeService.updateById(type);
        if (flag) {
            attributes.addFlashAttribute("message", "更新失败");
        } else {
            attributes.addFlashAttribute("message", "更新成功");
        }
        return "redirect:/admin/types";
        
    }
    
    
}
